package com.xigua.xcar.net.getnearbydata;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.util.Log;

import com.bantouyan.json.Json;
import com.bantouyan.json.JsonArray;
import com.bantouyan.json.JsonObject;
import com.xigua.xcar.constant.LocationPoint;
import com.xigua.xcar.data.ShopListData;
import com.xigua.xcar.helper.CalDistance;
import com.xigua.xcar.net.AbstractData;
import com.xigua.xcar.net.AbstractDataManager;
import com.xigua.xcar.util.CommonListUtil;

public class GetNearbyDataManager extends AbstractDataManager {

	private static final String tag = "GetNearbyDataManager";
	private int longitude = 0;
	private int latitude = 0;
	private int requestcode = 1045;
	private String param = null;
	private GetNearbyData mData = null;
	private int searchType = 0;	//1为任意搜索
								//2表示加油站
								//3表示洗车
	private int pageIndex = 0;
	private int isNearby = 0;	//0表示不附带附近，1表示附带附近
	
	public GetNearbyDataManager(Context context) {
		super(context);
	}
	
	public void setLocation(int lon, int lat){
		longitude = lon;
		latitude = lat;
	}
	
	public void setIsNearby(int flag){
		isNearby = flag;
	}
	
	public void setSearchKey(String keyWord){
		param = keyWord;
	}
	
	public void setSearchType(int type){
		searchType = type;
		if(1 == searchType){
			requestcode = 1045;
		}else if(2 == searchType || 3 == searchType){
			requestcode = 1047;
			param = "" + (type-1);
		}
	}
	
	@Override
	public List<NameValuePair> getRequestParam() {
		List<NameValuePair> postParamList = new ArrayList<NameValuePair>();
		BasicNameValuePair nameValuePair1  = new BasicNameValuePair("requestcode",""+requestcode);        
        postParamList.add(nameValuePair1);
        nameValuePair1 = new BasicNameValuePair("param1", ""+longitude);
        Log.i(tag, "param1:longitude:" + longitude);
        postParamList.add(nameValuePair1);
        nameValuePair1 = new BasicNameValuePair("param2", ""+latitude);
        Log.i(tag, "param2:latitude:" + latitude);
        postParamList.add(nameValuePair1);
        nameValuePair1 = new BasicNameValuePair("param3", param);
        postParamList.add(nameValuePair1);
        nameValuePair1 = new BasicNameValuePair("param4", pageIndex+"");
        postParamList.add(nameValuePair1);
        nameValuePair1 = new BasicNameValuePair("param5", isNearby+"");
        postParamList.add(nameValuePair1);
		return postParamList;
	}

	public void setPageIndex(int index){
		pageIndex = index;
	}
	
	public int getPageIndex(){
		return pageIndex;
	}

	@Override
	public AbstractData getData() {
		return mData;
	}

	@Override
	public AbstractData afterParseData(String ss) {
		Log.i(tag, "orign:ss:" + ss);
		String array[] = ss.split(";");
		if(null != array){
			if(null != array[0]){
				ss = array[0];
			}
		}
		ss = ss.replace("\n", "");
		if(ss.indexOf("(")!=-1){
			ss = ss.substring(1);
		}
		if(ss.indexOf(")")!= -1){
			ss = ss.substring(0, ss.length()-1);
		}
		Log.i(tag, "ss:" + ss);
		try {
			JsonArray jsonArray = (JsonArray)Json.parseJsonText(ss);
			if(jsonArray.count()>0){
			mData = new GetNearbyData();
			ArrayList<ShopListData> aa = new ArrayList<ShopListData>();
			for(int i=0; i<jsonArray.count(); i++){
				JsonObject obj = jsonArray.getJsonObject(i);
				String shopId = obj.getString("stationid");			//店铺唯一标识
				String addr = obj.getString("addr");
//				String typeid = obj.getString("typeid");			//
				String stationname = obj.getString("stationname");	//店名
//				String logopath = obj.getString("logopath");		//logo路径
				String markPrice = obj.getString("markPrice");		//价格
				String longitude = obj.getString("longitude");		//经度
				String latitude = obj.getString("latitude");		//纬度
				ShopListData data = new ShopListData();	
				if(null == markPrice || "".equals(markPrice) || "null".equals(markPrice) || "0".equals(markPrice)){
					data.setSaleInfo(addr);
				}else{
					data.setSaleInfo(markPrice);
				}
				data.setShopName(stationname);
				data.setShopNum(Integer.parseInt(shopId));
				Double distance = 0.0;
				if((0 == CommonListUtil.getLocationPointFromSP(mContext).getLatitude() || 0 == CommonListUtil.getLocationPointFromSP(mContext).getLontitude())){
//					distance = CalDistance.gps2m(Double.parseDouble(latitude), Double.parseDouble(longitude), 120000000, 30000000);
					data.setDistance("");
				}else{
					LocationPoint loc = CommonListUtil.getLocationPointFromSP(mContext);
//					distance = CalDistance.getDistance(Double.parseDouble(longitude), Double.parseDouble(latitude), loc.getLatitude()*1e6, loc.getLongitude()*1e6);
					distance = CalDistance.gps2m(Double.parseDouble(latitude), Double.parseDouble(longitude), loc.getLatitude(), loc.getLontitude());
					data.setDistance((distance.intValue())+"");
				}
				
				aa.add(data);
			}
			mData.setmArray(aa);
		}else{
			mData = null;
		}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			mData = null;
		}
		return mData;
	}

}
